define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, $timeout, $filter, Myagent_order, $stateParams, GoPage, $window) {
		$scope.input = new Object();
		$scope.input.store_id = $stateParams.id;
		$scope.input.order_id = '';
		$scope.input.goods_name = '';
		$scope.input.ordertype = 'finish';
		$scope.input.type_items = [{id:"finish",name:"已完成"},
		{id:"nopayed",name:"待付款"},{id:"ship",name:"待发货"},{id:"dead",name:"已取消"},
		{id:"confirm",name:"待确认"},{id:"refunded",name:"已退货"}];
		$scope.input.type_select = {id:0,name:"请选择订单状态"};
		$scope.input.start_date = '';
		$scope.input.end_date = '';
		$scope.input.ngPage = 1;
		$scope.data = new Object();
		$scope.time = '';
		$scope.order_count = 0;
		$scope.order_money = 0;
		var start_time = '';
		var end_time = '';
		$scope.count = null;
		$scope.$on('$ionicView.afterEnter', function () {
			//画面加载
			var lan = 0; //这是状态栏，只有ios的APP才有
			var tab = 0;
			var bars = document.getElementById("nav-bar-title");
			var bar = ((bars != undefined) ? 44 : 0);
			if (window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
			//整体高度
			var all = $window.innerHeight;
			var myorder_top1 = document.getElementById("myorder-top1").clientHeight;
			$scope.myorder_myObj = { //滚动条可视范围
				"height": all - lan - bar - tab - myorder_top1 - 20 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.doSelectType = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择订单状态",$scope.input.type_items,$scope.input.type_select,function(data){
				$scope.input.ordertype = data.id;
				$scope.input.type_select = data;
			});
		}
		$scope.isLoad = false;
		$scope.init_data = function () {
			//统一做法
			if ($scope.input.start_date != '')
				start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if ($scope.input.end_date != '')
				end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if (end_time != '' && start_time != '')
				$scope.time = start_time + '~' + end_time;

			var params = {
				store_id: $scope.input.store_id,
				order_id: $scope.input.order_id,
				goods_name: $scope.input.goods_name,
				time: $scope.time,
				ordertype: $scope.input.ordertype,
				nPage: 1
			};
			Myagent_order.myagent_list(params, function (data) {
				if (data != null) {
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					$scope.data = data;
					if (data.order_money == null)
						$scope.order_money = 0;
				} else {
					$scope.data = new Object();
					$scope.order_count = 0;
					$scope.order_money = 0;
				}
				$scope.input.ngPage = 1;
			});
		};
		//检索数据
		$scope.search = function () {
			if ($scope.input.start_date != '')
				start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if ($scope.input.end_date != '')
				end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if (end_time != '' && start_time != '')
				$scope.time = start_time + '~' + end_time;

			//统一做法
			var params = {
				store_id: $scope.input.store_id,
				order_id: $scope.input.order_id,
				goods_name: $scope.input.goods_name,
				time: $scope.time,
				ordertype: $scope.input.ordertype,
				nPage: 1
			};
			//条件查询
			Myagent_order.myagent_list(params, function (data) {
				if (data != null) {
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					$scope.data = data;
					$scope.input.ngPage = 1;
					if (data.order_money == null)
						$scope.order_money = 0;
				}
				if (data == null) {
					$scope.order_count = 0;
					$scope.order_money = 0;
					$scope.data = new Object();
					$scope.input.ngPage = 1;
				}
			});
		};
		//防止重复加载
		//		$scope.isLoad = false;
		//下拉刷新
		$scope.doRefresh = function () {
			$scope.isLoad = true;

			if ($scope.input.start_date != '')
				start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if ($scope.input.end_date != '')
				end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if (end_time != '' && start_time != '')
				$scope.time = start_time + '~' + end_time;
			//统一做法
			var params = {
				store_id: $scope.input.store_id,
				order_id: $scope.input.order_id,
				goods_name: $scope.input.goods_name,
				time: $scope.time,
				ordertype: $scope.input.ordertype,
				nPage: 1
			};
			//条件查询
			Myagent_order.myagent_list(params, function (data) {
				if (data != null) {
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					$scope.data = data;
					$scope.input.ngPage = 1;
					if (data.order_money == null)
						$scope.order_money = 0;
				}
				if (data == null) {
					$scope.order_count = 0;
					$scope.order_money = 0;
					$scope.data = new Object();
					$scope.input.ngPage = 1;
				}
			}, function () {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function () {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function () {
			//防止重复加载
			$scope.isLoad = true;

			if ($scope.input.start_date != '')
				start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if ($scope.input.end_date != '')
				end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if (end_time != '' && start_time != '')
				$scope.time = start_time + '~' + end_time;
			if ($scope.data.order == undefined) {
				$scope.input.ngPage = 0;

			} else
				if ($scope.data.page.count <= $scope.data.order.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if ($scope.data.page.count < $scope.input.ngPage * 10) {
						$scope.input.ngPage -= 1;
						$scope.count = $scope.input.ngPage * 10;
					} else {
						$scope.count = null;
					}
				}

			//统一做法
			var params = {
				store_id: $scope.input.store_id,
				order_id: $scope.input.order_id,
				goods_name: $scope.input.goods_name,
				time: $scope.time,
				ordertype: $scope.input.ordertype,
				nPage: $scope.input.ngPage + 1
			};
			//条件查询
			Myagent_order.myagent_list(params, function (data) {
				//				console.log(data);
				if (data.order != null) {
					//数据合并
					if ($scope.data.order != undefined) {
						if ($scope.count != null)
							$scope.data.order.splice($scope.count, $scope.data.page.count - $scope.count);
						$scope.data.order = $scope.data.order.concat(data.order);
						$scope.input.ngPage = $scope.input.ngPage + 1;
					} else {
						$scope.order_count = data.order_count;
						$scope.order_money = data.order_money;
						$scope.data = data;
						$scope.input.ngPage = 1;
					}

				}
			}, function () {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function () {
					$scope.isLoad = false; //可以加载
				}, 1500);

			});
		};

		//页面跳转
		$scope.show_detail = function (orderNo, store_id) {
			GoPage.go_states("myagentdistributor_order", { "order_id": orderNo, "store_id": $scope.input.store_id, "user_type_id": 3 });
		};
		$scope.onshow_first = function (store_id) {
			GoPage.go_states("first", { "store_id": store_id });
		}

	}

	controller.$inject = ['$scope', '$timeout', '$filter', 'Myagent_order', '$stateParams', 'GoPage', '$window'];

	return app.registerController("myagent_orderctrl", controller);
});